package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.pgyersdk.crash.PgyCrashManager;
import com.pgyersdk.update.PgyUpdateManager;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.app.AppConstant;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.eventbus.TokenOvertimeEvent;
import com.shqcjd.yunweiyanoperator.model.pojo.UserControlBean;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.AppManager;
import com.shqcjd.yunweiyanoperator.util.PrefUtils;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 管理员首页
 */
public class AdministerMainActivity extends BaseActivity {


    @BindView(R.id.btn_administer_main)
    ImageButton btnAdministerMain;
    @BindView(R.id.ll_administer_staff)
    LinearLayout llAdministerStaff;
    @BindView(R.id.rl_administer_repair)
    RelativeLayout rlAdministerRepair;
    @BindView(R.id.rl_administer_share)
    RelativeLayout rlAdministerShare;
    @BindView(R.id.rl_administer_pwd)
    RelativeLayout rlAdministerPwd;
    @BindView(R.id.tv_administer_company)
    TextView tvAdministerCompany;
    @BindView(R.id.tv_administer_staff)
    TextView tvAdministerStaff;
    @BindView(R.id.tv_administer_balance)
    TextView tvAdministerBalance;
    @BindView(R.id.iv_administer_staff_tip)
    ImageView ivAdministerStaffTip;
    @BindView(R.id.rl_administer_review)
    RelativeLayout rlAdministerReview;
    @BindView(R.id.rl_department)
    RelativeLayout rl_department;


    private Context mContext;

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.topbgcolor2)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarDarkIcon(true)
                .init();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_administer_main;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
        Log.e("tou", PrefUtils.getString(mContext, PrefUtils.TOKEN, ""));
    }

    @Override
    public void initView() {
//        EventBus.getDefault().register(this);//订阅
//        initPgy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        userControl();
    }

    private void initPgy() {
        //蒲公英
        PgyCrashManager.register(); //推荐使用新版
        /** 蒲公英可选配置集成方式 **/
        new PgyUpdateManager.Builder()
                .setForced(false)                //设置是否强制提示更新
                // v3.0.4+ 以上同时可以在官网设置强制更新最高低版本；网站设置和代码设置一种情况成立则提示强制更新
                .setUserCanRetry(false)         //失败后是否提示重新下载
                .setDeleteHistroyApk(true)     // 检查更新前是否删除本地历史 Apk， 默认为true
                .register();
    }

    private void showOutDialog() {
        final MaterialDialog dialog = new MaterialDialog.Builder(this)
                .customView(R.layout.custome_out_view, false)
                .show();

        View customeView = dialog.getCustomView();

        TextView cancel = customeView.findViewById(R.id.tv_cancel);
        TextView confirm = customeView.findViewById(R.id.tv_confirm);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                signOut();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);//解除订阅
    }

    /**
     * 退出登录
     */
    private void signOut() {
        PrefUtils.setBoolean(this, PrefUtils.FIRST_LOGIN, false);
        PrefUtils.setInt(this, PrefUtils.USER_ID, -1);
        PrefUtils.setString(this, PrefUtils.USER_CODE, "");
        PrefUtils.setString(this, PrefUtils.USER_NAME, "");
        PrefUtils.setString(this, PrefUtils.PHONE_NUMBER, "");
        PrefUtils.setString(this, PrefUtils.USER_ICON, "");
        PrefUtils.setString(this, AppConstant.TOKEN_KEY, "");
        PrefUtils.setInt(this, PrefUtils.USER_TYPE, -1);
        PrefUtils.setString(this, PrefUtils.USER_ACCOUNT, "");
        PrefUtils.setString(this, PrefUtils.USER_PWD, "");
        PrefUtils.setString(this, PrefUtils.TOKEN, "");
        PrefUtils.setInt(this, PrefUtils.USER_STATE, -1);
        PrefUtils.setString(this, PrefUtils.BUSINESSId, "");
        PrefUtils.setString(this, PrefUtils.APPCODE, "");
        PrefUtils.setInt(mContext, PrefUtils.ORGANIZATION_TYPE, -1);
        PrefUtils.setString(mContext, PrefUtils.BUSINESSNAME, "");
        AppManager.getAppManager().finishAllActivity();
        Intent it = new Intent(this, LoginActivity.class);
        startActivity(it);
    }


    @OnClick({R.id.btn_administer_main, R.id.ll_administer_staff, R.id.rl_administer_repair,
            R.id.rl_administer_share, R.id.rl_administer_pwd, R.id.rl_administer_review,R.id.rl_department})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_administer_main:
                finish();
//                showOutDialog();
                break;
            case R.id.ll_administer_staff://员工管理
                Intent it = new Intent(mContext, StaffManageActivity.class);
                startActivity(it);
                break;
            case R.id.rl_administer_repair://维修列表
                Intent itRepair = new Intent(mContext, AdminRepairRecordActivity.class);
                startActivity(itRepair);
                break;
            case R.id.rl_administer_share://分享列表
                Intent itShare = new Intent(mContext, AdminShareActivity.class);
                startActivity(itShare);
                break;
            case R.id.rl_administer_pwd:
                Intent itPwd = new Intent(mContext, ChangePasswordActivity.class);
                startActivity(itPwd);
                break;
            case R.id.rl_administer_review:
                startActivity(new Intent(mContext, StaffReviewActivity.class));
                break;
            case R.id.rl_department:
                startActivity(new Intent(mContext,DepartmentManageActivity.class));
                break;
        }
    }

    private void userControl() {
        ApiService.userControl()
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<UserControlBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<UserControlBean> responseData) {
                        if (responseData.retCode == 10000) {
                            tvAdministerCompany.setText(responseData.data.businessName);
                            tvAdministerStaff.setText(responseData.data.employeeNum+"");
                            tvAdministerBalance.setText(responseData.data.balance+"");
                            if(responseData.data.isReviewRequest){
                                ivAdministerStaffTip.setVisibility(View.VISIBLE);
                            }else{
                                ivAdministerStaffTip.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onTokenOvertimeEvent(TokenOvertimeEvent event){//token超时，重新登录
//        ToastUtils.showLong(mContext,"登录信息过期,请重新登录");
//        signOut();
//    }

}

